package controller

import (
	"GO/common"
	"GO/model"
	"GO/tools"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Sign(c *gin.Context) {
	var sign model.Sign
	if err := c.ShouldBindJSON(&sign); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"status": 400,
			"msg":    "服务器存在问题，请稍后再试",
		})
		return
	}
	//用于接受数据库查询结果
	type flag struct {
		account string
	}
	if !tools.Verify(sign.Account, sign.Password) {
		c.JSON(http.StatusOK, gin.H{
			"status": 400,
			"msg":    "账号密码格式有问题",
		})
		return
	}
	var flag_ flag
	db := common.GetDB()
	//如果查询到账号(account)存在在数据库，返回300
	if err := db.Raw("SELECT account FROM user_information WHERE account = ?", sign.User_login.Account).Scan(&flag_); err.RowsAffected != 0 {
		c.JSON(http.StatusOK, gin.H{
			"status": 300,
			"msg":    "账号已存在",
		})
		return
	}
	if err := db.Exec("INSERT INTO user_information(account,username,password) VALUES(?,?,?)", sign.Account, sign.Username, sign.Password); err.Error != nil {
		c.JSON(http.StatusOK, gin.H{
			"status": 400,
			"msg":    "服务器存在问题，请稍后再试",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"status": 200,
		"msg":    "注册成功",
	})
}
